#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "user/oled.h"
#include "user/ina220.h"
#include "user/menu.h"
#include "adc.h"
#include "user/QC_Get.h"

#define voltage ina220_data[0]
#define current ina220_data[1]
#define power ina220_data[2]
#define energy ina220_data[3]
u16 ina220_data[4]={0,0,0,0};
double energy_temp=0;
u8 key_enter=0,key_up=0,key_down=0,key_back=0;
u8 adj_up=0,adj_down=0;

u8 time_1s_ok=0;
u8 func_index=0;

u16 mem_addr_base=0x0000;
u16 mem_addr_base_r=0x0000;

u8 oled_on=1;
u8 data_sw=0;

void SystemClock_Config(void);
static void MX_NVIC_Init(void);

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  MX_USART2_UART_Init();
  MX_TIM3_Init();
  MX_TIM14_Init();

  MX_NVIC_Init();
  HAL_TIM_Base_Start_IT(&htim14);
  HAL_TIM_Base_Start_IT(&htim3);
  Clear_mem();

  OLED_Init_096();		//初始化OLED
  INA220_Init();		//初始化ina220
  key_back=key_enter=key_up=key_down=0;

  while (1)
  {

	  OLED_Refresh();
	  if(time_1s_ok)
	  {
		  time_1s_ok=0;
		  voltage     = INA220_GetVoltage();
		  current     = INA220_GetCurrent();
		  power       = INA220_GetPower();
		  energy_temp = energy_temp + power*0.00027777;
		  energy      = energy_temp;

		  if(power!=0&&mem_addr_base<0xE00)
		  {
			  HAL_I2C_Mem_Write(&hi2c1,0xA0,mem_addr_base,I2C_MEMADD_SIZE_16BIT,(u8*)ina220_data,4,100);
			  mem_addr_base=mem_addr_base+4;
		  }
	  }

	  /*******************find index****************************/
	  if((key_enter==1)||(key_up==1)||(key_down==1))
	  {
		  if(key_up==1)
		  {
			  func_index=table[func_index].up;    //up
			  key_up=0;
		  }
		  if(key_down==1)
		  {
			  func_index=table[func_index].down;    //down
			  key_down=0;
		  }
		  if(key_enter==1)
		  {
			  OLED_Clear();
			  func_index=table[func_index].enter;    //enter
			  key_enter=0;
		  }

	  }
	  if(key_back==1)
	  {
		  key_back=0;
		  if(oled_on==1)
			  oled_on=0;
		  else
			  oled_on=1;
	  }
	  current_operation_index=table[func_index].current_operation;
	  (*current_operation_index)();

	  HAL_Delay(10);

  }
}

void SystemClock_Config(void)
{
  LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
  while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
  {
  }

  LL_RCC_HSI_Enable();
  while(LL_RCC_HSI_IsReady() != 1)
  {
  }

  LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, LL_RCC_PLLM_DIV_1, 9, LL_RCC_PLLR_DIV_3);
  LL_RCC_PLL_Enable();
  LL_RCC_PLL_EnableDomain_SYS();
  while(LL_RCC_PLL_IsReady() != 1)
  {
  }

  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);

  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  {
  }

  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  LL_SetSystemCoreClock(48000000);

  if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
  {
    Error_Handler();
  }
}

static void MX_NVIC_Init(void)
{

  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 3, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
  HAL_NVIC_SetPriority(TIM3_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(TIM3_IRQn);
  HAL_NVIC_SetPriority(TIM14_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(TIM14_IRQn);
}

void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);
	__HAL_TIM_ENABLE(&htim14);

	if(GPIO_Pin==GPIO_PIN_4)
	{
		key_back=1;
		if(func_index==9)
			data_sw=1;
	}
	if(GPIO_Pin==GPIO_PIN_5)
	{
		key_down=1;
		if(func_index==5||func_index==9||func_index==10||func_index==11)//向下调节
			adj_down=1;
	}
	if(GPIO_Pin==GPIO_PIN_6)
	{
		key_up=1;
		if(func_index==5||func_index==9||func_index==10||func_index==11)//向上调节
			adj_up=1;
	}
	if(GPIO_Pin==GPIO_PIN_7)
	{
		key_enter=1;
		if(func_index==10||func_index==11)//退出QC
			QC_DeInit();
		if(func_index==9)
			mem_addr_base_r=0;
	}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim == &htim3)
	{
		time_1s_ok=1;//1s时间到
	}
	if(htim == &htim14)
	{
		HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
	}
}
void Error_Handler(void)
{
  __disable_irq();
  while (1)
  {
  }

}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
